<?php

namespace app\system\model;

use think\Model;
use think\facade\Request;
use think\facade\Config;
use think\helper\Str;

/**
 * 登录日志模型
 */
class LogLogin extends Model
{
    /**
     * 数据表名称
     * @var string
     */
    protected $name = 'system_log_login';

    /**
     * 获取操作系统
     * @param string $userAgent 用户代理字符串
     * @return string 操作系统名称
     */
    public static function getOS($userAgent)
    {
        $oses = array(
            'Windows 10' => 'Windows NT 10.0',
            'Windows 8.1' => 'Windows NT 6.3',
            'Windows 8' => 'Windows NT 6.2',
            'Windows 7' => 'Windows NT 6.1',
            'Windows Vista' => 'Windows NT 6.0',
            'Windows XP' => 'Windows NT 5.1|Windows NT 5.2',
            'Windows 2000' => 'Windows NT 5.0',
            'Windows ME' => 'Win 9x 4.90',
            'Windows 98' => 'Windows 98|Win98',
            'Windows 95' => 'Windows 95',
            'Windows 3.11' => 'Win3.11',
            'Android' => 'Android',
            'iOS' => 'iPhone|iPad|iPod',
            'Mac OS X' => 'Mac OS X',
            'Mac OS 9' => 'Mac OS 9',
            'Linux' => 'Linux',
            'Unix' => 'X11'
        );
        foreach ($oses as $os => $pattern) {
            if (preg_match('/' . $pattern . '/i', $userAgent)) {
                return $os;
            }
        }
        return '未知操作系统';
    }

    /**
     * 获取浏览器
     * @param string $userAgent 用户代理字符串
     * @return string 浏览器名称
     */
    public static function getBrowser($userAgent)
    {
        $browsers = array(
            'Firefox' => 'Firefox',
            'Chrome' => 'Chrome',
            'Safari' => 'Safari',
            'Opera' => 'Opera|OPR',
            'IE' => 'MSIE|Trident/7.0',
            'Edge' => 'Edge',
            'Netscape' => 'Netscape',
            'Maxthon' => 'Maxthon',
            'Konqueror' => 'Konqueror',
            'UCBrowser' => 'UCBrowser',
            'SamsungBrowser' => 'SamsungBrowser',
            'Vivaldi' => 'Vivaldi',
        );
        foreach ($browsers as $browser => $pattern) {
            if (preg_match('/' . $pattern . '/i', $userAgent)) {
                return $browser;
            }
        }
        return '未知浏览器';
    }


    /**
     * 新增登录日志
     * @param string $username 用户名
     * @param string $msg 日志信息
     * @param string $browser 浏览器名称
     * @param string $os 操作系统名称
     */
    public static function add($username, $msg, $browser = '', $os = '')
    {
        $open = Config::get('xp.log');
        if ($open) {
            $obj = new self;
            $obj->save([
                'username' => $username,
                'ip' => Request::ip(),
                'browser' => $browser ? $browser : self::getBrowser(Request::header('user-agent')),
                'os' => $os ? $os : self::getOS(Request::header('user-agent')),
                'status' => Str::contains($msg, '成功') ? 0 : 1,
                'msg' => $msg,
                'dateline' => time(),
            ]);
        }
    }
}
